package com.sunwayworld.common.utils;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Expression;

import com.sunwayworld.common.hibernate.HibernateSessionFactory;

public class QueryHibernateUtils {
	public static VCommList getVCommList(Class _class, Map map, SplitPage spage) {
		try {

			Session session = HibernateSessionFactory.currentSession();
			Criteria criteria = session.createCriteria(_class);
			criteria = copyCriteria(_class, map, criteria);
			int nCount = 0;
			List list = null;
			if (null == spage) {
				list = criteria.list();
				nCount = list.size();
				return fillCommList(list, spage, nCount);
			}
			list = criteria.list();
			nCount = list.size();
			list = null;
			int _first = (spage.getCurrPage() - 1) * spage.getNumPerPage();
			//int _last = spage.getCurrPage() * spage.getNumPerPage();
			int _max=spage.getNumPerPage();

			criteria.setFirstResult(_first);
			criteria.setMaxResults(_max);
			list = criteria.list();
			return fillCommList(list, spage, nCount);
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}

	private static Criteria copyCriteria(Class _class, Map map,
			Criteria criteria) {
		if (map == null)
			return criteria;
		Method[] methods = _class.getMethods();

		for (int i = 0; i < methods.length; i++) {
			String methodName = methods[i].getName();
			if ("get".equals(methodName.substring(0, 3))) {
				String _name = methodName.substring(3, methodName.length());
				_name = StringUtils.tofirstLowerCase(_name, 1);

				if (map.containsKey(_name)) {
					String _value = StringUtils.toString(map.get(_name));
					System.out.println("==value=="+_value);
					if (!"".equals(_value)) {
						// Class _name_class = methods[i].getClass();
						// Object val = BeanUtils.infoEncode(_value,
						// _name_class);
						Criterion t = Expression
								.like(_name, "%" + _value + "%");
						criteria.add(t);
					}
				}

			}

		}
		return criteria;
	}

	/**
	 * 
	 * @param vlist
	 *            取回的list
	 * @param spage
	 * @param nCount
	 *            总条数
	 */
	@SuppressWarnings("unchecked")
	private static VCommList fillCommList(List list, SplitPage spage, int nCount) {

		if (list == null) {
			return null;
		}
		VCommList vlist = new VCommList();
		vlist.addAll(list);
		if (spage == null) {
			spage = new SplitPage();
			spage.setNumPerPage(SplitPage.CONST_NUMPERPAGE);
			spage.setCurrPage(SplitPage.CONST_CURRPAGE);
		}
		vlist.setNumPerPage(spage.getNumPerPage());
		vlist.setCurrPage(spage.getCurrPage());
		vlist.setNum(nCount);
		vlist.setPageNum(nCount / vlist.getNumPerPage());
		if ((vlist.getPageNum() * vlist.getNumPerPage()) < nCount)
			vlist.setPageNum(vlist.getPageNum() + 1);

		return vlist;
	}
}
